﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace PulseApp
{
    public class TimeSeriesPoint
    {

        public TimeSeriesPoint(double[] values)
        {
            myHashCode = 0;
            measurements = new double[values.Length];
            for (int x = 0; x < values.Length; x++) {
                myHashCode += ((Double)(values[x])).GetHashCode();
                measurements[x] = values[x];
            }
        }
        public TimeSeriesPoint(ICollection values) {
            measurements = new double[values.Count];
            myHashCode = 0;
            IEnumerator i = values.GetEnumerator();
            for (int index = 0; i.MoveNext(); index++) {
                Object nextElement = i.Current;
                if (nextElement is Double)
                    measurements[index] = ((double) nextElement);
                else if (nextElement is int)
                    measurements[index] = ((double)nextElement);
                else if (nextElement is Int32)
                    measurements[index] = ((double) nextElement);
                else if (nextElement is Int64)
                    measurements[index] = ((double)nextElement);
                else
                    throw new Exception("ERROR:  The element " + nextElement + " is not a valid numeric type");
                myHashCode += ((Double)(measurements[index])).GetHashCode();
            }
        }

        public double get(int dimension)
        {
            return measurements[dimension];
        }

        public void set(int dimension, double newValue)
        {
            myHashCode -= ((double)(measurements[dimension])).GetHashCode();
            measurements[dimension] = newValue;
            myHashCode += ((double)(newValue)).GetHashCode();
        }


        public double[] toArray()
        {
            return measurements;
        }

        public int size()
        {
            return measurements.Length;
        }

        public String toString()
        {
            String outStr = "(";
            for (int x = 0; x < measurements.Length; x++)
            {
                outStr = outStr + measurements[x];
                if (x < measurements.Length - 1)
                    outStr = outStr + ",";
            }

            outStr = outStr + ")";
            return outStr;
        }

        public bool equals(Object o) 
        {
            if (this == o)
                return true;
            if (o is TimeSeriesPoint) {
                double[] testValues = ((TimeSeriesPoint)o).toArray();
                if (testValues.Length == measurements.Length) {
                    for (int x = 0; x < measurements.Length; x++)
                        if (measurements[x] != testValues[x])
                            return false;

                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }

        public int hashCode()
        {
            return myHashCode;
        }

        private double[] measurements;
        private int myHashCode;
    }
}
